package com.example.portalfundacao;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import banco.DBAdapter;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button button = (Button) findViewById(R.id.button1);
        
//        DBAdapter db = new DBAdapter(getApplicationContext());
//        db.open();
//        db.insertUsuario("admin", toMD5("teste"));
//        db.close();
        
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				EditText passInput = (EditText) findViewById(R.id.editText1);
				EditText loginInput = (EditText) findViewById(R.id.editText2);
				
				String pass = passInput.getText().toString();
				String login = loginInput.getText().toString();
				
				if (pass.trim().length() != 0 && login.trim().length() != 0) {
					DBAdapter db = new DBAdapter(getApplicationContext());
					db.open();
					try {
						Cursor cursor = db.getUsuario(login);
						if (cursor != null && cursor.moveToFirst()) {
							String passBD = cursor.getString(2);
							
							if (passBD.equals(toMD5(pass))) {
								Intent intent = new Intent(MainActivity.this, MenuPrincipalActivity.class);
								startActivity(intent);
								
								Toast.makeText(getApplicationContext(), "Bem Vindo, " + login + "!", Toast.LENGTH_LONG).show();
							} else
								Toast.makeText(getApplicationContext(), "Senha Inválida!", Toast.LENGTH_LONG).show();
							
						} else
							Toast.makeText(getApplicationContext(), "Login Inválido!", Toast.LENGTH_LONG).show();
					} catch (Exception e) {
						e.printStackTrace();
					}
					finally {
						db.close();
					}
				} else
					Toast.makeText(getApplicationContext(), "Informe Login e Senha!", Toast.LENGTH_LONG).show();
			}
		});
        
    }
    
    /**
	 * Transforma a String passada em MD5 sem usar salt.
	 *
	 * @param senha
	 * @return
	 */
	public static String toMD5(String senha) {
		return toMD5(senha, null);
	}
	
	/**
	 * Transforma a String passada em MD5 utilizando um salt.
	 * @param senha
	 * @param salt
	 * @return
	 */
	public static String toMD5(String senha, String salts) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
			
			if (salts != null && salts.trim().length() != 0) {
				String salttmp[] = salts.split(",");
			    byte salt[] = new byte[salttmp.length];

			    for (int i = 0; i < salt.length; i++) {
			      salt[i] = Byte.parseByte(salttmp[i]);
			    }
			    
			    md.update(salt);
			}

			BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));
			String s = hash.toString(16);

			while (s.length() < 32)
				s = "0" + s;

			return s;

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
